<?php
namespace app\admin\logic;

use app\admin\model\system\SystemDept;
use app\admin\model\system\SystemRoleDept;
use app\admin\model\system\SystemUser;
use app\common\exception\ApiException;
use think\facade\Db;

class BaseLogic extends \app\common\basic\BaseLogic
{
    /**
     * @var object 管理员信息
     */
    protected $adminInfo;
    
    /**
     * @var bool 数据边界启用状态
     */
    protected $scope = false;
    
    // 所有数据权限
    public const ALL_SCOPE = 1;
    // 自定义数据权限
    public const CUSTOM_SCOPE = 2;
    // 本机构数据权限
    public const SELF_DEPT_SCOPE = 3;
    // 本机构及子机构数据权限
    public const DEPT_BELOW_SCOPE = 4;
    // 本人数据权限
    public const SELF_SCOPE = 5;
    
    /**
     * @var array 用户id
     */
    public $userIds = [];

    /**
     * 初始化
     * @param $user
     * @return void
     */
    public function init($user): void
    {
        $this->adminInfo = $user;
    }
    
    /**
     * 设置数据边界
     * @param $scope
     * @return void
     */
    public function setScope($scope)
    {
        $this->scope = $scope;
    }
    
    /**
     * 数据权限处理
     * @param $query
     * @return mixed
     */
    public function userDataScope($query, $field = 'created_by', $adminInfo = null): mixed
    {
        if (empty($this->adminInfo) && empty($adminInfo)) {
            throw new ApiException('数据权限读取失败');
        }
        if (empty($this->adminInfo) && !empty($adminInfo)) {
            $this->adminInfo = $adminInfo;
        }
        
        foreach ($this->adminInfo['roleList'] as $role) {
            $data_scope = $role['data_scope'];
            $role_id = $role['id'];
            
            switch ($data_scope) {
                case self::ALL_SCOPE: // 所有数据权限
                    return $query;
                case self::CUSTOM_SCOPE: // 自定义数据权限
                    $userIds = SystemUser::alias('user')->whereExists(function ($query) use ($role_id) {
                        $query->table(SystemRoleDept::getTable())->alias('role_dept')->where('role_dept.role_id', $role_id)->whereRaw('user.dept_id = role_dept.dept_id');
                    })->column('id');
                    $this->userIds = array_merge($this->userIds, $userIds);
                    break;
                case self::SELF_DEPT_SCOPE:
                    $deptId = $this->adminInfo['dept_id'];
                    $userIds = SystemUser::where('dept_id', $deptId)->column('id');
                    $this->userIds = array_merge($this->userIds, $userIds);
                    break;
                case self::DEPT_BELOW_SCOPE:
                    $deptId = $this->adminInfo['dept_id'];
                    $deptInfo = SystemDept::where('id', $deptId)->find();
                    $level = $deptInfo['level'].",".$deptId;
                    $deptIds = SystemDept::where(function ($query) use($level) {
                        $query->where('level', $level)->whereOr('level', 'like', $level . ',%');
                    })->column('id');
                    $deptIds[] = $deptId;
                    $userIds = SystemUser::where('dept_id', 'in', $deptIds)->column('id');
                    $this->userIds = array_merge($this->userIds, $userIds);
                    break;
                case self::SELF_SCOPE:
                    $this->userIds = array_merge($this->userIds, [$this->adminInfo['id']]);
                    break;
                default:
                    break;
            }
        }
        return $query->where($field, 'in', array_unique($this->userIds));
    }
}
